// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cstdint>
#include <memory>
#include <vector>

#include <nebula/array/data.h>
#include <nebula/core/compare.h>

#include <turbo/utility/status.h>
#include <nebula/types/type.h>
#include <turbo/base/macros.h>

namespace nebula {

    /// \defgroup array-factories Array factory functions
    ///
    /// @{

    /// \brief create a strongly-typed Array instance from generic ArrayData
    /// \param[in] data the array contents
    /// \return the resulting Array instance
    TURBO_EXPORT
    std::shared_ptr<Array> make_array(const std::shared_ptr<ArrayData> &data);

    /// \brief create a strongly-typed Array instance with all elements null
    /// \param[in] type the array type
    /// \param[in] length the array length
    /// \param[in] pool the memory pool to allocate memory from
    TURBO_EXPORT
    turbo::Result<std::shared_ptr<Array>> MakeArrayOfNull(const std::shared_ptr<DataType> &type,
                                                          int64_t length,
                                                          MemoryPool *pool = default_memory_pool());

    /// \brief create an Array instance whose slots are the given scalar
    /// \param[in] scalar the value with which to fill the array
    /// \param[in] length the array length
    /// \param[in] pool the memory pool to allocate memory from
    TURBO_EXPORT
    turbo::Result<std::shared_ptr<Array>> MakeArrayFromScalar(
            const Scalar &scalar, int64_t length, MemoryPool *pool = default_memory_pool());

    /// \brief create an empty Array of a given type
    ///
    /// The output Array will be of the given type.
    ///
    /// \param[in] type the data type of the empty Array
    /// \param[in] pool the memory pool to allocate memory from
    /// \return the resulting Array
    TURBO_EXPORT
    turbo::Result<std::shared_ptr<Array>> MakeEmptyArray(std::shared_ptr<DataType> type,
                                                         MemoryPool *pool = default_memory_pool());

    /// @}

    namespace internal {

        /// \brief Swap endian of each element in a generic ArrayData
        ///
        /// As dictionaries are often shared between different arrays, dictionaries
        /// are not swapped by this function and should be handled separately.
        ///
        /// \param[in] data the array contents
        /// \param[in] pool the memory pool to allocate memory from
        /// \return the resulting ArrayData whose elements were swapped
        TURBO_EXPORT
        turbo::Result<std::shared_ptr<ArrayData>> SwapEndianArrayData(
                const std::shared_ptr<ArrayData> &data, MemoryPool *pool = default_memory_pool());

        /// Given a number of ArrayVectors, treat each ArrayVector as the
        /// chunks of a chunked array.  Then rechunk each ArrayVector such that
        /// all ArrayVectors are chunked identically.  It is mandatory that
        /// all ArrayVectors contain the same total number of elements.
        TURBO_EXPORT
        std::vector<ArrayVector> RechunkArraysConsistently(const std::vector<ArrayVector> &);

    }  // namespace internal
}  // namespace nebula
